package com.aurora.oa.work.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;
import com.aurora.oa.work.bean.ApproveDataBean;
import com.aurora.oa.work.presenter.ApproveDetailActivityPresenter;
import com.aurora.oa.work.presenter.ApproveDetailActivityPresenterImpl;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/24 0024.
 */
public class ApproveDetailActivity extends BaseActivity implements ApproveDetailActivityView {
    String id;
    RequestHttpModel model = new RequestHttpModelImpl();
    @Bind(R.id.sdv_head_image)
    SimpleDraweeView sdvHeadImage;
    @Bind(R.id.tv_title)
    AppCompatTextView tvName;
    @Bind(R.id.tv_creat_time)
    AppCompatTextView tvCreatTime;
    @Bind(R.id.tv_postname)
    AppCompatTextView tvPostname;
    @Bind(R.id.tv_approve_state)
    AppCompatTextView tvApproveState;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.ll_option)
    LinearLayout llOption;
    @Bind(R.id.sv_content)
    ScrollView svContent;
    @Bind(R.id.bt_agree)
    Button btAgree;
    @Bind(R.id.bt_deni)
    Button btDeni;
    @Bind(R.id.bt_transfer)
    Button btTransfer;
    @Bind(R.id.bt_comment)
    Button btComment;
    boolean isView;

    private ApproveDetailActivityPresenter presenter = new ApproveDetailActivityPresenterImpl(this);
    private String transferIds;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "审批详情", true);
        setMyContentView(R.layout.activity_approve_detail);
        ButterKnife.bind(this);
        getData();
    }

    private void getData() {
        id = getIntent().getStringExtra("id");
        isView =getIntent().getBooleanExtra("is_view",false);
        presenter.displayApproveData(id);
    }

    @OnClick({R.id.bt_agree, R.id.bt_deni, R.id.bt_transfer, R.id.bt_comment})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_agree:
                presenter.doAgree(id);
                break;
            case R.id.bt_deni:
                presenter.doDeni(id);
                break;
            case R.id.bt_transfer:
                startActivityForResult(new Intent(ApproveDetailActivity.this, CompanyStructureListActivity.class), 0);
                break;
            case R.id.bt_comment:
                final EditText editText = new EditText(this);
                new AlertDialog.Builder(this)
                        .setTitle("请输入")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(editText)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                presenter.doComment(id, editText.getText().toString());
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
                break;
        }
    }

    @Override
    public void displayApproveData(ApproveDataBean approveDataBean) {
        if (null != approveDataBean) {
            ApproveDataBean.HeadDataBean headDataBean = approveDataBean.getHeadDataBean();
            sdvHeadImage.setImageURI(AuroraConstants.BASE_SERVER_URL + "/" + headDataBean.getIco());
            tvName.setText(headDataBean.getName());
            tvCreatTime.setText(headDataBean.getCreateTime());
            tvPostname.setText(headDataBean.getPostName());
            switch (headDataBean.getApprovalState()) {
                case 1:
                    tvApproveState.setText("未审核");
                    if(isView){
                        llOption.setVisibility(View.GONE);
                    }else{
                        llOption.setVisibility(View.VISIBLE);
                    }

                    break;
                case 2:
                    tvApproveState.setText("已通过");
                    llOption.setVisibility(View.GONE);
                    break;
                case 3:
                    tvApproveState.setText("已拒绝");
                    llOption.setVisibility(View.GONE);
                    break;
            }
            //初始化键值对
            List<ApproveDataBean.CommonContent> commonContentList = approveDataBean.getCommonContentBeanList();
            for (int i = 0, size = commonContentList.size(); i < size; i++) {

                View keyValueView = View.inflate(ApproveDetailActivity.this, R.layout.item_key_value, null);
                TextView key = (TextView) keyValueView.findViewById(R.id.tv_key);
                TextView value = (TextView) keyValueView.findViewById(R.id.tv_remark);
                key.setText(commonContentList.get(i).getTitle());
                if (null == commonContentList.get(i).getValue() || commonContentList.get(i).getValue().equals("")) {
                    value.setText("暂无数据");
                } else {
                    value.setText(commonContentList.get(i).getValue());
                }
                llContent.addView(keyValueView);
            }
        }
    }

    @Override
    public void onGetApproveDataError(String message) {
        ToastUtil.show(this, message);
    }

    @Override
    public void optionSuccess(String msg) {

        ToastUtil.show(this,msg);
        finish();
    }



    @Override
    public void optionFail(String msg) {
        ToastUtil.show(this,msg);
    }

    @Override
    public void showMessage(String msg) {
        ToastUtil.show(this,msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //选择人员
        if (resultCode == RESULT_OK) {
            if (requestCode == 0) {
                transferIds = data.getStringExtra("who_to_send_ids");
                // (List<CompanyStructureBean.ChildNodeBean>) data.getSerializableExtra("contacts_list"));
                presenter.doTransfer(id,transferIds);
            }
        }

    }
}
